package com.batch.demo.domain.itemReaderDemo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class ItemReaderDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job itemReaderJob(){
        return jobBuilderFactory.get("itemReaderJob")
                .start(itemReaderStep())
                .build();

    }

    @Bean
    public Step itemReaderStep(){
        return stepBuilderFactory.get("itemReaderStep")
                .chunk(2)
                .reader(itemReaderDemoReader())
                .writer(list->{
                    for(Object item : list){
                        System.out.println("item:"+item);
                    }
                }).build();
    }

    public MyItemReader itemReaderDemoReader(){
        List<String> data = Arrays.asList("dog","cat","bird");
        return new MyItemReader(data);
    }
}
